--[[--
        消息转发函数
--]]--

local skynet      = require "skynet"
local manager     = require "manager"


local M = {}

function M:open(handle, fd, addr)

    if  handle and fd and addr then 
        skynet.send(handle,"lua", "socket", "open", fd, addr)
        return false  
    end 

    return true
end 

-- 转发到代理agent服务上
function M:agent(handle, client, msg, sz)

    if handle and msg and sz then 

        skynet.redirect(handle, client, "client", 1, msg, sz)
        return false
    end 

    return true 
end 

-- 转发到指定的 func_1 func_2 指定的函数
function M:message(handle, func_1, func_2, fd, msg, sz)

    if handle and func_1 and func_2 and fd and msg and sz then 

        skynet.send(handle,"lua", func_1, func_2, fd, msg, sz)
        return false 
    end 

    return true 
end 


-- 创建管理器添加到全局管理器中
function M:create_manager(name)

    if not name  then 
        return true 
    end 

    local result = skynet.call("global_manager","lua","get", name)

    -- 存在则返回false
    if result then 
        return false 
    end 
    
    local mgr = manager:new()
    
    if not mgr then 
        return true 
    end 

    -- 添加到全局管理器中
     skynet.call("global_manager", "lua", "add", name, mgr)  

     return false        
end 


return M